package com.tg.demo.service.impl;

import com.tg.demo.common.util.StringUtils;
import com.tg.demo.config.SysUserContext;
import com.tg.demo.entity.Hobby;
import com.tg.demo.entity.Score;
import com.tg.demo.mapper.HobbyMapper;
import com.tg.demo.mapper.ScoreMapper;
import com.tg.demo.service.HobbyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 用户信息表ServiceImpl
 *
 * @author yuanjin
 */
@Service
public class HobbyServiceImpl implements HobbyService {
    private static final Logger logger = LoggerFactory.getLogger(HobbyServiceImpl.class);

    @Resource
    private HobbyMapper hobbyMapper;

    @Resource
    private ScoreMapper scoreMapper;


    @Override
    public Hobby recommendValue(String userHobbies) {
        if (StringUtils.isBlank(userHobbies)) {
            return Hobby.builder().build();
        }
        String[] hobbies = userHobbies.split("、");
        List<Hobby> all = new ArrayList<>();
        for (String ele : hobbies) {
            List<Hobby> hobbyList = hobbyMapper.getListByCondition(Hobby.builder().type(ele).build());
            all.addAll(hobbyList);
        }
        Score score = Score.builder().userCode(SysUserContext.getSysUser().getUserCode()).build();
        List<Score> list = scoreMapper.getListByCondition(score);
        if (null != list && list.size() == 1) {
            score = list.get(0);
            String itemName = this.getMinScoreName(score);
            List<Hobby> hobbyList = hobbyMapper.getListByCondition(Hobby.builder().code(itemName).build());
            if (null != hobbyList && hobbyList.size() > 0){
                for (int i = 0; i < 3; i++) {
                    all.addAll(hobbyList);
                }
            }
        }
        if (all.isEmpty()) {
            return Hobby.builder().build();
        }
        int index = (int) (Math.random() * (all.size()));
        return all.get(index);
    }

    private String getMinScoreName(Score score) {
        String name = "computerApplicationFoundation";
        double value = Double.parseDouble(score.getComputerApplicationFoundation());
        if (Double.parseDouble(score.getProfessionalRequired()) < value) {
            name = "professionalRequired";
            value = Double.parseDouble(score.getProfessionalRequired());
        }
        if (Double.parseDouble(score.getEnglish()) < value) {
            name = "english";
            value = Double.parseDouble(score.getEnglish());
        }
        if (Double.parseDouble(score.getMath()) < value) {
            name = "math";
            value = Double.parseDouble(score.getMath());
        }
        if (Double.parseDouble(score.getMarxism()) < value) {
            name = "marxism";
            value = Double.parseDouble(score.getMarxism());
        }
        if (Double.parseDouble(score.getChinaSpecial()) < value) {
            name = "chinaSpecial";
            value = Double.parseDouble(score.getChinaSpecial());
        }
        if (Double.parseDouble(score.getChinaHistory()) < value) {
            name = "chinaHistory";
        }
        return name;
    }
}
